{include file="public/header" /}
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>
            <table id="munu-table" class="layui-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

{include file="public/footer" /}
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'id',
            treePidName: 'pid',
            elem: '#munu-table',
            url: "{:url("admin/menu/getlist")}",
            page: false,
            cols: [[
                {type: 'numbers'},
                {field: 'title', minWidth: 100, title: '权限名称'},
                {field: 'condition', title: '权限标识'},
                {field: 'name', title: '菜单url'},
                {field: 'sort', width: 80, align: 'center', title: '排序号'},
                {
                    field: 'type', width: 80, align: 'center', templet: function (d) {
                        if (d.pid == 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                 {templet: '#auth-state', width: 200, align: 'center', title: '操作'}
                /*{templet:  function (d) {
                        if (d.pid == 0) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '操作'
                }*/
            ]],


            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                //layer.msg('删除' + data.id);
                layer.confirm('确定删除该菜单吗', function (index) {
                    //obj.del();
                    $.ajax({
                        url: "{:url('del')}",
                        type: 'post',
                        dataType: 'json',
                        contentType: 'application/json',
                        data: JSON.stringify({id: data.id}),
                        timeout: 2000,
                        success: function (data1) {
                            if (data1.code == 0) {
                                layer.msg(data1.msg)
                            } else {
                                layer.msg(data1.msg)
                            }
                        },
                        error: function () {
                            layer.msg("删除失败")
                        }
                    })
                    layer.close(index);
                    location.reload();
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '修改菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/menu/edit")}?id=' + data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            }else if (layEvent === 'add') {
                var index = layer.open({
                    title: '添加菜单',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '{:url("admin/menu/add")}?pid=' + data.id,
                });

                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>